package com.simple.simple_view.service.impl;

import com.simple.simple_view.domain.ResponseResult;
import com.simple.simple_view.domain.entity.Video;
import com.simple.simple_view.domain.system.RedisSystemParam;
import com.simple.simple_view.domain.vo.HotVideoInfo;
import com.simple.simple_view.service.HotVideoService;
import com.simple.simple_view.service.VideoService;
import com.simple.simple_view.utils.BeanCopyUtil;
import com.simple.simple_view.utils.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/*
 *@description: 视频热榜服务实现类
 *@author: hisen
 *@Time: 2023/11/1  2:06
 */
@Component
@Slf4j
public class HotVideoServiceImpl implements HotVideoService {
    @Resource
    RedisCache redisCache;
    @Resource
    VideoService videoService;

    @Scheduled(cron = "0 0 0 * * ?") // 每天的00:00执行
    public void clearDayHotList() {
        redisCache.deleteObject(RedisSystemParam.DAY_HOT_VIDEO_KEY);
    }
    @Scheduled(cron = "0 0 0 ? * MON") // 每周一的00:00执行
    public void clearWeekHotList() {
        redisCache.deleteObject(RedisSystemParam.WEEK_HOT_VIDEO_KEY);
    }
    @Override
    public void addVideoToHotList(Long videoId, HotVideoInfo hotVideo) {
        redisCache.addToZSet(RedisSystemParam.DAY_HOT_VIDEO_KEY, videoId, 0); // 初始化分数为0
        redisCache.addToZSet(RedisSystemParam.WEEK_HOT_VIDEO_KEY, videoId, 0); // 初始化分数为0
    }
    @Override
    public void incrementScore(String member, double delta){
        redisCache.incrementScore(RedisSystemParam.DAY_HOT_VIDEO_KEY, member, delta);
        redisCache.incrementScore(RedisSystemParam.WEEK_HOT_VIDEO_KEY, member, delta);
    }
    @Override
    public ResponseResult getTopNHotVideos(int start, int end, String type) {
        List<String> hotVideo = redisCache.getHotVideo(start, end,type);
        List<String>keys=hotVideo.stream().map(id->type+id).collect(Collectors.toList());
        List<HotVideoInfo> videos = redisCache.getObjectList(keys);
        for (int i = 0; i < videos.size(); i++) {
            if(videos.get(i)==null){
                Video video = videoService.getById(hotVideo.get(i));
                if(video!=null){
                    HotVideoInfo hotVideoInfo = BeanCopyUtil.copyBean(video, HotVideoInfo.class);
                    hotVideoInfo.setHot(video.getLikeCount()+video.getCommentCount()+video.getFavoriteCount());
                    videos.set(i, hotVideoInfo);
                    redisCache.setCacheObject(RedisSystemParam.VIDEO_PREFIX+hotVideo.get(i), video);
                    redisCache.expire(keys.get(i),RedisSystemParam.HOT_VIDEO_EXPIRES, TimeUnit.MINUTES);
                }
            }
        }
        return ResponseResult.okResult(videos);
    }

    @Override
    public void batchIncrementScore(List<Long> videoIdSet, int score) {
        videoIdSet.forEach(id->{incrementScore(RedisSystemParam.VIDEO_PREFIX+id.toString(),score);});
    }
}
